﻿using MySql.Data.MySqlClient;
using System;
using System.Data.Common;
using XuanFe.Database.Common;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.Database.BLL
{
    public class SequenceBLL : AbstractBase
    {
        private static SequenceBLL instance = new SequenceBLL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static SequenceBLL Instance
        {
            get
            {
                return instance;
            }
        }
        public SequenceBLL() : base() { }
        /// <summary>
        /// 用于序列号同步
        /// </summary>
        private object syncObj = new object();
        /// <summary>
        /// 获取序列号
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public decimal GetSeqNextValue(string name)
        {
            lock (syncObj)
            {
                using (DbConnection db = MySQLHelper.Instance.CreateConnection())
                {
                    try
                    {
                        db.Open();

                        using (DbTransaction tran = db.BeginTransaction())
                        {
                            try
                            {
                                int row = SequenceDAL.Instance.Lock(tran, name);
                                if (row <= 0)
                                {
                                    throw new Exception(string.Format("找不到序列号“{0}”", name));
                                }
                                SequenceModel info = SequenceDAL.Instance.Get(tran, name);
                                if (info == null)
                                {
                                    throw new Exception(string.Format("找不到序列号“{0}”", name));
                                }
                                decimal result = info.NextNumber;
                                info.NextNumber += info.IncrementBy;
                                if (info.NextNumber > info.MaxValue)
                                {
                                    if (info.Cycle)
                                    {
                                        // 循环
                                        info.NextNumber = info.MinValue;
                                    }
                                    else
                                    {
                                        // 不循环
                                        throw new Exception(string.Format("序列号“{0}”超出最大长度", name));
                                    }
                                }
                                row = SequenceDAL.Instance.Update(tran, info);
                                return result;
                            }
                            catch
                            {
                                throw;
                            }
                            finally
                            {
                                tran.Commit();
                            }
                        }
                    }
                    finally
                    {
                        db.Close();
                    }
                }
            }
        }
    }
}
